﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using CUSTOMS.Components;
using System.Web.UI.WebControls;
using CUSTOMS.Modules.QLHS.Components.QLCT;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using CUSTOMS.Modules.QLHS.Components;

namespace CUSTOMS.Modules.QLHS.Controls
{
    public partial class AddEditChungTu : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonCancel = btnCancel;

            this.Update += new EventHandler(AddEditChungTu_Update);
            base.OnInit(e);
        }

        void AddEditChungTu_Update(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                ChungTu chungtu = new ChungTu();
                chungtu.SoToKhai = Convert.ToInt32(Request.QueryString["sotokhai"]);
                chungtu.MaHaiQuan = Request.QueryString["mahaiquan"];
                chungtu.MaLoaiHinh = Request.QueryString["maloaihinh"];
                chungtu.NamDangKy = Convert.ToInt32(Request.QueryString["namdangky"]);
                chungtu.PhienBan = Guid.NewGuid();

                chungtu.SoTo = Convert.ToInt32(txtSoTo.Text);

                chungtu.NoiGiao = ddlNoiGiao.SelectedValue;
                chungtu.NguoiGiao = txtNguoiGiao.Text;

                chungtu.NoiNhan = ddlNoiNhan.SelectedValue;
                chungtu.NguoiNhan = txtNguoiNhan.Text;

                int gio = Convert.ToInt32(ddlGio.SelectedValue);
                int phut = Convert.ToInt32(ddlPhut.SelectedValue);
                DateTime ngay = Convert.ToDateTime(txtNgay.Text);
                chungtu.NgayBanGiao = new DateTime(ngay.Year, ngay.Month, ngay.Day, gio, phut, 0);

                chungtu.GhiChu = txtGhiChu.Text;

                chungtu.NgayNhapDuLieu = DateTime.Now;
                chungtu.NguoiNhapDuLieu = PortalSecurity.CurrentUser.Identity.Email;

                chungtu.Insert();

                string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.TheoDoiChungTu);
                Response.Redirect(url);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                txtMaLoaiHinh.Text = Request.QueryString["maloaihinh"];
                txtmaHaiQuan.Text = Request.QueryString["mahaiquan"];
                txtSoToKhai.Text = Request.QueryString["sotokhai"];
                txtHaiQuan.Text = DanhMuc.GetTenDonViHaiQuan(Request.QueryString["mahaiquan"]);
                txtLoaiHinh.Text = DanhMuc.GetTenLoaiHinhMauDich(Request.QueryString["maloaihinh"]);
                txtNamDangKy.Text = Request.QueryString["namdangky"];

                ddlNoiGiao.DataSource = BoPhan.SelectAll();
                ddlNoiGiao.DataBind();

                ddlNoiNhan.DataSource = BoPhan.SelectAll();
                ddlNoiNhan.DataBind();

                // Ngày, giờ
                for (int d = 0; d < 24; d++)
                {
                    ddlGio.Items.Add(new ListItem(d.ToString(), d.ToString()));
                }

                for (int m = 0; m < 60; m++)
                {
                    ddlPhut.Items.Add(new ListItem(m.ToString(), m.ToString()));
                }

                DateTime now = DateTime.Now;
                txtNgay.Text = now.ToShortDateString();

                ddlGio.SelectedValue = now.Hour.ToString();
                ddlPhut.SelectedValue = now.Minute.ToString();
            }
        }
    }
}